package org.spring.demo.controller;

import javax.servlet.http.HttpServletRequest;

import org.spring.demo.model.User;
import org.spring.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;



@Controller
@RequestMapping(value="/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@RequestMapping(value="/register", method=RequestMethod.GET)
	public String toRegister(){
		return "register";
	}
	
	@RequestMapping(value="/register", method=RequestMethod.POST)
	public ModelAndView register(String account,String password){
		/*String account=request.getParameter("account");
		String password=request.getParameter("password");*/
		userService.addUser(account, password); 
		ModelAndView mav = new ModelAndView();
		mav.addObject("account",account);
		mav.setViewName("index");
		//request.setAttribute("account", account);
		// 省略其他逻辑... 
		return mav;
	}
	
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public ModelAndView login(String account,String password) {
		User user = userService.findUserByAccount(account);
		ModelAndView mav = new ModelAndView();
		mav.addObject("message", message);
		mav.setViewName("message");
		String message;
		if(user!=null){
			if(password.equals(user.getPassword())){
				message = "ok";
			}else{
				message = "password error";
			}
		}else{
			message = "user is not registered";
		}
		return mav;
	}
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login(){
		return "login";
	}
	
}
